package org.lex.reflect.browsing.view;

import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;

import org.lex.reflect.browsing.view.browser.ConstructorTableModel;
import org.lex.reflect.browsing.view.browser.FieldTableModel;
import org.lex.reflect.browsing.view.browser.MethodTableModel;
import org.lex.reflect.browsing.view.browser.TypeHierarchyPane;
import org.lex.swing.Swings;
import org.lex.swing.widgets.AutoResizeTable;


public class ClassMembersPane extends JPanel {
	private JTabbedPane tabbedPane;
	private TypeHierarchyPane hierarchyPane;
	private JTable fieldTable;
	private JTable methodTable;
	private JTable constructorTable;
	private JScrollPane fieldScrollPane;
	private JScrollPane methodScrollPane;
	private JScrollPane constructorScrollPane;
	// ============================
	private ConstructorTableModel constructorTableModel = null;
	private MethodTableModel methodTableModel = null;
	private FieldTableModel fieldTableModel = null;

	public static void main(String[] args) {
		Swings.setUseBoldMetalFont(false);
		ClassMembersPane pane = new ClassMembersPane();
		pane.setClass(Vector.class);
		Swings.show(pane, false);
	}

	public ClassMembersPane() {
		super(new BorderLayout());
		this.initialize();
	}

	public void setClass(Class<?> clazz) {
		this.getHierarchyPane().setClass(clazz);
		this.getConstructorTableModel().setClass(clazz);
		this.getMethodTableModel().setClass(clazz);
		this.getFieldTableModel().setClass(clazz);
	}

	private void initialize() {
		this.add(this.getTabbedPane(), BorderLayout.CENTER);
	}

	private JTabbedPane getTabbedPane() {
		if (null == tabbedPane) {
			tabbedPane = new JTabbedPane();
			tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
			tabbedPane.setName(this.getClass().getName() + ".tab");
			tabbedPane.addTab("Hierarchy", this.getHierarchyPane());
			tabbedPane.addTab("Constructors", null, getConstructorScrollPane(), null);
			tabbedPane.addTab("Methods", null, getMethodScrollPane(), null);
			tabbedPane.addTab("Fields", null, getFieldScrollPane(), null);
		}
		return tabbedPane;
	}

	private TypeHierarchyPane getHierarchyPane() {
		if (null == hierarchyPane)
			hierarchyPane = new TypeHierarchyPane();
		return hierarchyPane;
	}

	private JScrollPane getConstructorScrollPane() {
		if (constructorScrollPane == null) {
			constructorScrollPane = new JScrollPane();
			constructorScrollPane.setViewportView(getConstructorTable());
		}
		return constructorScrollPane;
	}

	private JScrollPane getMethodScrollPane() {
		if (methodScrollPane == null) {
			methodScrollPane = new JScrollPane();
			methodScrollPane.setViewportView(getMethodTable());
		}
		return methodScrollPane;
	}

	private JScrollPane getFieldScrollPane() {
		if (fieldScrollPane == null) {
			fieldScrollPane = new JScrollPane();
			fieldScrollPane.setViewportView(getFieldTable());
		}
		return fieldScrollPane;
	}

	private JTable getConstructorTable() {
		if (constructorTable == null) {
			constructorTable = new AutoResizeTable(this.getConstructorTableModel());
			constructorTable.setName(this.getClass().getName() + ".constructor");
			constructorTable.setFillsViewportHeight(true);
			constructorTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		}
		return constructorTable;
	}

	private ConstructorTableModel getConstructorTableModel() {
		if (null == constructorTableModel)
			constructorTableModel = new ConstructorTableModel();
		return constructorTableModel;
	}

	private JTable getMethodTable() {
		if (methodTable == null) {
			methodTable = new AutoResizeTable(this.getMethodTableModel());
			methodTable.setName(this.getClass().getName() + ".method");
			methodTable.setFillsViewportHeight(true);
			methodTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		}
		return methodTable;
	}

	private MethodTableModel getMethodTableModel() {
		if (null == methodTableModel)
			methodTableModel = new MethodTableModel();
		return methodTableModel;
	}

	private JTable getFieldTable() {
		if (fieldTable == null) {
			fieldTable = new AutoResizeTable(this.getFieldTableModel());
			fieldTable.setName(this.getClass().getName() + ".field");
			fieldTable.setFillsViewportHeight(true);
			fieldTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		}
		return fieldTable;
	}

	private FieldTableModel getFieldTableModel() {
		if (null == fieldTableModel)
			fieldTableModel = new FieldTableModel();
		return fieldTableModel;
	}
}
